import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DataProcessing {
    private static int max_reports = 25;       //前推delta_time时间, 取出最近的25条报告
    private static long delta_time = 3 * 30 * 24 * 60 * 60;     // 3个月的秒数, 作为时间戳的差值


    public static Map<String, List<String>> getMsgAll(String filePath) throws FileNotFoundException, IOException, ParseException{
        File file = new File(filePath);

//        try{
//            List<String> msgAll = FileUtils.readLines(file);
//            for(int i = 0; i < msgAll.size(); i++){
//
//            }
//        }catch (IOException e){
//            e.printStackTrace();
//        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        Map<String, List<String>> bug_msg_all = new HashMap<>();
        String line = "";
        while ((line = br.readLine()) != null){
            List<String> temp = Arrays.asList(line.split("\t"));
            //java中getTime时间戳生成的是毫秒级, 所以需要除以1000
            temp.set(4, ""+new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(temp.get(4).split(" -")[0]).getTime()/1000);
            temp.set(5, ""+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(temp.get(5).split(" -")[0]).getTime()/1000);
            bug_msg_all.put(temp.get(0), temp.subList(1, temp.size()));
        }
        br.close();
        return bug_msg_all;
    }
    /**
     * 抽出指定数量的开发者组成开发者活跃度列表, 顺序由远到近**/
    private List<String> extractAssigners(Map<String, String> id_deltaTs, Map<String, List<String>> bug_msg_all){
        //根据value排序id_deltaTs
        //先将map转换为ArrayList
        List<Map.Entry<String, String>> results_list = new ArrayList<>(id_deltaTs.entrySet());
        Collections.sort(results_list, new Comparator<Map.Entry<String, String>>() {
            @Override
            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                return (int)(Long.parseLong(o1.getValue()) - Long.parseLong(o2.getValue()));
            }
        });

        //取出后25个, 即最近的25个
        //如果没有25条呢.......
        //暂时不管了, 包括一条都没有的, 全部予以写入对应文件
        if (results_list.size() > max_reports){
            results_list = results_list.subList(results_list.size()-max_reports, results_list.size());

        }else{
            //维持原状不做处理
        }

        //按顺序输出列表, 从小到大输出
//        for (int i = 0 ; i< results_list.size(); i++){
//            System.out.println(results_list.get(i).getKey() + "\t" + results_list.get(i).getValue());
//        }
        List<String> assigners = new ArrayList<>();
//        if (results_list.size() != 0){
        //将results_list根据bug_id转换成对应的assigner
        for (int i = 0 ; i< results_list.size(); i++){
            String c_id = results_list.get(i).getKey();
            String assigner = bug_msg_all.get(c_id).get(0);
            assigners.add(assigner);
        }
//        }

        return assigners;


    }
    /**
     * 完成针对一个bug的开发者活跃度提取工作**/
    public static Map<String, String> getDeveloperActiveSequence(String id, Map<String, List<String>> bug_msg_all, String path) throws Exception{


        List<String> bug = bug_msg_all.get(id);
        String product = bug.get(5);
        String component = bug.get(6);
        long creation_ts = Long.parseLong(bug.get(3));
        System.out.println(creation_ts);
        //原先考虑用key=delta_ts, value=assign_to, 但是想到这样key会有重复值
        //我去, 倒过来也不行........
        //那就key=id, value=delta_ts, 根据value排完序之后根据id找对应assign_to
        Map<String, String> results = new HashMap<>();  //
        for(Map.Entry<String, List<String>> entry: bug_msg_all.entrySet()){
            List<String> value = entry.getValue();
            if (value.get(5).equals(product)  && value.get(6).equals(component) && Long.parseLong(value.get(4))>=(creation_ts-delta_time) && Long.parseLong(value.get(4)) <= creation_ts){
                results.put(entry.getKey(), entry.getValue().get(4));
            }
        }

        List<String> assigners = new DataProcessing().extractAssigners(results, bug_msg_all);
        //assigners按照时间顺序写入文件
        File file = new File(path + "" + id);
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (int i = 0;i<assigners.size(); i++){
            bw.write(assigners.get(i) + "\n");
        }
        bw.close();

        return results;
    }
    /**
     * 遍历全部bug, 对每一个bug执行getDeveloperActiveSequence处理**/
    public static void traverseAllBugs(Map<String, List<String>> bug_msg_all, String file, String assignees_path) throws Exception{
        Map<String, Integer> actives = new HashMap<>();
        int count = 0;
        for (Map.Entry<String, List<String>> entry: bug_msg_all.entrySet()){
            Map<String, String> active = getDeveloperActiveSequence(entry.getKey(), bug_msg_all, assignees_path);
            actives.put(entry.getKey(), active.size());
            System.out.println(count);
            count += 1;
        }
        //将每条bug的活跃度列表的长度写入文件
        //这里是一开始做原型测试的时候用的,
        File output = new File(file);
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        for (Map.Entry<String, Integer> entry: actives.entrySet()){
            bw.write(entry.getKey() + "\t" + entry.getValue() + "\n");
        }
        bw.close();
    }

}
